package ne.analyzer.parts;

import java.util.List;

import javax.annotation.PostConstruct;

import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;

import ne.analyzer.entities.Reference;
import ne.analyzer.importer.NEImporter;
import ne.analyzer.ui.DataTable;
import ne.analyzer.ui.DataTableInf;
import ne.analyzer.ui.StepPanel;
import ne.analyzer.ui.UserInfoPanel;
import ne.analyzer.util.Consts;
import ne.analyzer.util.ImportAction;
import ne.analyzer.util.Utils;

/**
 * 分析系统主界面
 * 
 * @author sunny
 *
 */
public class MainPagePart {

	@PostConstruct
	public void createComposite(Composite parent) throws Exception {
		parent.setLayout(new FormLayout());
		FormData data = new FormData();
		data.left = new FormAttachment(0, 5);
		data.top = new FormAttachment(0, 150);
		data.bottom = new FormAttachment(100, -5);
		data.right = new FormAttachment(100, -5);
		DataTable refTable = new DataTable(parent, SWT.NONE, new DataTableInf() {

			@Override
			public int[] getWidths() {
				return new int[] { 180, 120, 200, 200, 100 };
			}

			@Override
			public String[] getColumns() {
				return new String[] { "标题", "作者", "关键词", "主题词", "来源" };
			}

			@Override
			public String getColumnText(Object element, int columnIndex) {
				Reference ref = (Reference) element;
				String value = null;
				switch (columnIndex) {
				case 0:
					value = ref.getTitle();
					break;
				case 1:
					value = Utils.join(ref.getAuthors());
					break;
				case 2:
					value = Utils.join(ref.getKeywords());
					break;
				case 3:
					value = Utils.join(ref.getSubjects());
					break;
				case 4:
					value = ref.getSource();
					break;
				}
				return value;
			}
		});
		refTable.setLayoutData(data);

		// ---------------------------------
		Composite toolComp = new Composite(parent, SWT.NONE);
		FormData fa = new FormData();
		fa.left = new FormAttachment(0, 0);
		fa.right = new FormAttachment(100, -5);
		fa.top = new FormAttachment(0, 5);
		fa.bottom = new FormAttachment(refTable, -5);
		toolComp.setLayoutData(fa);
		toolComp.setLayout(new GridLayout(6, false));
		UserInfoPanel uip = new UserInfoPanel(toolComp, SWT.NONE, "用户，您好");
		uip.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true));
		StepPanel sp1 = new StepPanel(toolComp, SWT.NONE, "第一步：数据", "支持NoteExpress/EndNote\n/RIS等格式数据") {

			@Override
			public Composite createContent(Composite content) {
				Label startButton = new Label(content, SWT.CENTER);
				content.setLayout(new GridLayout(1, true));
				startButton.setText("打开");
				startButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
				startButton.setImage(Utils.getImageFromClassPath("/icons/open.png"));
				startButton.setCursor(new Cursor(null, SWT.CURSOR_HAND));
				startButton.addMouseListener(new MouseListener() {

					@Override
					public void mouseUp(MouseEvent e) {
						FileDialog fdlg = new FileDialog(content.getShell(), SWT.OPEN);
						String refFilePath = fdlg.open();
						if (refFilePath != null) {
							// MessageDialog.openInformation(parent.getShell(),
							// "导入文件", refFilePath);
							ImportAction impAct = new ImportAction(new NEImporter());
							String content = Utils.file2String(refFilePath, Consts.UTF8);
							impAct.extract(content);
							List<Reference> refers = impAct.convert();
							refTable.setReferences(refers);
						}
					}

					@Override
					public void mouseDown(MouseEvent e) {

					}

					@Override
					public void mouseDoubleClick(MouseEvent e) {

					}
				});
				return content;
			}

		};
		sp1.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true));
		StepPanel sp2 = new StepPanel(toolComp, SWT.NONE, "第二步：字段", "") {

			@Override
			public Composite createContent(Composite content) {
				content.setLayout(new GridLayout(1, false));
				Combo comFd1 = new Combo(content, SWT.SIMPLE);
				String[] fields = new String[] { "全部", "关键词", "主题词" };
				comFd1.setItems(fields);
				Combo comFd2 = new Combo(content, SWT.SIMPLE);
				comFd2.setItems(fields);
				return content;
			}

		};
		sp2.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true));

		StepPanel sp3 = new StepPanel(toolComp, SWT.NONE, "第三步：粒度", "") {

			@Override
			public Composite createContent(Composite content) {
				content.setLayout(new GridLayout(1, false));
				Button top10 = new Button(content, SWT.RADIO);
				top10.setText("前10");
				Button top20 = new Button(content, SWT.RADIO);
				top20.setText("前20");
				Button top50 = new Button(content, SWT.RADIO);
				top50.setText("前50");
				return content;
			}

		};
		sp3.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true));

		StepPanel sp4 = new StepPanel(toolComp, SWT.NONE, "第四步：方法", "") {

			@Override
			public Composite createContent(Composite content) {
				content.setLayout(new GridLayout(2, false));
				Button ndfbjz = new Button(content, SWT.RADIO);
				ndfbjz.setText("年度分布矩阵");
				Button gxt = new Button(content, SWT.RADIO);
				gxt.setText("关系图");
				Button jcjz = new Button(content, SWT.RADIO);
				jcjz.setText("交叉矩阵");
				Button ljfx = new Button(content, SWT.RADIO);
				ljfx.setText("路径分析");
				Button xgxsjz = new Button(content, SWT.RADIO);
				xgxsjz.setText("相关系数矩阵");
				Button jlfx = new Button(content, SWT.RADIO);
				jlfx.setText("聚类分析");
				return content;
			}

		};
		sp4.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true));
		StepPanel sp5 = new StepPanel(toolComp, SWT.NONE, "", "") {

			@Override
			public Composite createContent(Composite content) {
				content.setLayout(new GridLayout(1, false));
				Label startBtn = new Label(content, SWT.CENTER);
				startBtn.setImage(Utils.getImageFromClassPath("/icons/start.png"));
				startBtn.setCursor(new Cursor(null, SWT.CURSOR_HAND));
				return content;
			}

		};
		sp5.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
	}

	@Focus
	public void setFocus() {
	}

	@Persist
	public void save() {
	}
}